/* *
 * @program: VideoDirector
 *
 * @description: ${description}
 *
 * @author: 909845
 *
 * @create: 2018-12-26 19:25
***/

#include <sstream>

#include "Typedef.h"

const Int32 CConstVal::N_VIDEO_FRAME = 25;
const Int32 CConstVal::N_TS_PACK_SIZE = 188;
const Int32 CConstVal::N_TS_SEND_SIZE = CConstVal::N_TS_PACK_SIZE * 7;

const string CConstVal::STR_STREAM_ID    = "stream_id";
const string CConstVal::STR_RECORD       = "record";
const string CConstVal::STR_HTTP_OK      = "200 OK";
const string CConstVal::STR_ADDRESS      = "address";


const string SSchemeDefine::HTTP = "http";
const string SSchemeDefine::RTMP = "rtmp";
const string SSchemeDefine::RTSP = "rtsp";


string CUrlDefine::GenUrl()
{
    ostringstream oss;
    oss << strScheme << "://" << strHost << strPath << "/" << strID;
    if (!strQuery.empty())
    {
        oss << "?" << strQuery;
    }
    return oss.str();
}
// http://10.9.217.32:11300/publish/1234567?record=0
Bool CUrlDefine::ParseUrl(const string &strUrl, CUrlDefine &urlDefine)
{
    Bool bRet = False;
    do
    {
        list<string> lstSchemeAndOther;
        string::size_type nPos = string::npos;
        if (string::npos == (nPos = strUrl.find("://")))
        {
            ERROR_LOG("ParseUrl failed, not found " << "://"  << "  strUrl = " << strUrl);
            break;
        }
        urlDefine.strScheme = strUrl.substr(0, nPos); // http
        const string strHostAndPath = strUrl.substr(nPos + string("://").size()); // 10.9.217.32:11300/publish/1234567?record=0
        if (string::npos == (nPos = strHostAndPath.find("/")))
        {
            ERROR_LOG("ParseUrl failed, after '://' not found " << "/"  << "  strUrl = " << strUrl);
            break;
        }
        urlDefine.strHost = strHostAndPath.substr(0, nPos);// 10.9.217.32:11300
        const string strPathAndOther = strHostAndPath.substr(nPos); // /publish/1234567?record=0
        string strPathAndId;
        if ((nPos = strPathAndOther.find("?")) != string::npos)
        {
            strPathAndId = strPathAndOther.substr(0, nPos); // /publish/1234567
            urlDefine.strQuery = strPathAndOther.substr(nPos + string("?").size());
        }
        else
        {
            strPathAndId = strPathAndOther; // /publish/1234567
        }
        if (string::npos == (nPos = strPathAndId.find_last_of("/")))
        {
            ERROR_LOG("ParseUrl failed, not fund ID in " << strPathAndId << "  url =" << strUrl);
            break;
        }
        urlDefine.strPath = strPathAndId.substr(0, nPos); // /publish
        urlDefine.strID = strPathAndId.substr(nPos + string("/").size()); // 1234567

        bRet = True;
    }while (0);
    return bRet;
}
